<div class="headertitle">
    <label><h2>Pesan</h2></label>
</div>
<?php
$prefix = "";
if (isset($_GET['ajax'])) {
    session_start();
    $prefix = "../";
}

include_once $prefix . 'controller/registrasi.php';
include_once $prefix . 'controller/login.php';
include_once $prefix . 'controller/follow.php';
include_once $prefix . 'controller/admin.php';
include_once $prefix . 'controller/message.php';
?>
<?php
if (isset($_GET['name_viewed'])!= NULL) {
    $name_viewed = $_GET['name_viewed'];
}
else if (isset($_SESSION['user_name'])!= NULL) {
    $name_viewed = $_SESSION['user_name'];
}
if (isset($_SESSION['user_name'])!= NULL) {
    $name_logon = $_SESSION['user_name'];
    $temp = new login();
    $role = $temp->getRole($name_logon );
}
else {
    $name_logon = NULL;
    $role = NULL;
}
?>
<?php
$message = new message();

if ((isset($_GET['deleteMsgGroupID']) != NULL) && (isset($_GET['msgOwner']) != NULL)) {
    $message->delMsg($_GET['deleteMsgGroupID'], $_GET['msgOwner']);
}
// get page number
if(isset ($_GET['page'])) {
    $page = $_GET['page'];
}
else {
    $page = 1;
    
}
// calculate number of las page
$rows_per_page = 5;
$num_rows = $message->getNumRowMsg($name_logon);
$last_page = ceil($num_rows / $rows_per_page);

// ensure page number
$page = (int)$page;
if($page > $last_page) {
    $page = $last_page;
}
if($page < 1) {
    $page= 1;
}
?>
<div class="isipagestandar">
    <div class="kanan">
        <div>
            <a href="create_message.php">Create new message</a>
        </div>
    </div>

    <div>
        <label id="titlebold">Recent Messages</label>
    </div>
    <div class="space">
    </div>
    <div id="message_location">
        <?php
        $resultMsg = $message->showMsgByPagination($name_logon, $rows_per_page, $page);
        $iterate=(int)($page - 1) * (int)$rows_per_page;

        while($row = mysql_fetch_array($resultMsg)) {
            ?>
        <div class="boxListMessages">
                <?php
                $iterate++;
                $subject = $row['subject'];
                $from = $row['from'];
                $to = $message->showGroup($row['group_id']);
                $body = $row['body'];
                ?>
            <label id="bold"><?php echo $subject.' at '.$row['post_date'].' '.$row['post_time'] ?></label>
            <div>
                <label>From: </label>
                <label><?php echo $from ?></label>
            </div>
            <div>
                <label>To: </label>
                <label><?php echo $to ?></label>
            </div>
            <div class="space"></div>
            <div>
                <label id="bold"><?php echo $body ?></label>
            </div>
            <div class="space"></div>
            <div class ="reply">
                    <?php
                    echo'<a href="create_message.php?replyMsgID='.$row['msg_id'].'"><img src="images/replyMsg.png" alt="replyMsgButton" height="25" width="63" border="0"/></a>';
                    ?>
            </div>
            <div class ="delete">
                    <?php
                    echo'<a href="message.php?deleteMsgGroupID='.$row['group_id'].'&msgOwner='.$name_logon.'"><img src="images/deleteMsg.png" alt="deleteMsgButton" height="25" width="63" border="0"/></a>';
                    ?>
            </div>
            <div class="space"> </div>
            <div class="space"> </div>
            <div class="boxListReplyMessage">
                    <?php
                    $resultReplyMsg = $message->showReplyMsg($name_logon,$row['msg_id']);
                    if (mysql_num_rows($resultReplyMsg) > 0) {
                        echo '<div>';
                        echo '<label id="titlebold">Reply</label>';
                        echo '<div class="space"></div>';
                        echo '</div>';
                        echo '<div class="space"></div>';
                    }
                    $iterate=0;
                    while($rowRe = mysql_fetch_array($resultReplyMsg)) {
                        ?>
                <div>
                            <?php
                            $iterate++;
                            $subject = $rowRe['subject'];
                            $from = $rowRe['from'];
                            $to = $message->showGroup($rowRe['group_id']);
                            $body = $rowRe['body'];
                            ?>
                    <label id="bold"><?php echo $subject.' at '.$row['post_date'].' '.$row['post_time'] ?></label>
                    <div>
                        <label>From: </label>
                        <label><?php echo $from ?></label>
                    </div>
                    <div>
                        <label>To: </label>
                        <label><?php echo $to ?></label>
                    </div>
                    <div class="space"></div>
                    <div>
                        <label id="bold"><?php echo $body ?></label>
                    </div>
                    <div class="space"></div>
                    <div class ="reply">
                                <?php
                                echo'<a href="create_message.php?replyMsgID='.$row['msg_id'].'"><img src="images/replyMsg.png" alt="replyMsgButton" height="25" width="63" border="0"/></a>';
                                ?>
                    </div>
                    <div class ="delete">
                                <?php
                                echo'<a href="message.php?deleteMsgGroupID='.$row['group_id'].'&msgOwner='.$name_logon.'"><img src="images/deleteMsg.png" alt="deleteMsgButton" height="25" width="63" border="0"/></a>';
                                ?>
                    </div>
                    <div class="space"> </div>
                </div>
                        <?php } ?>
            </div>
            <div class="space"> </div>
            <div class="space"> </div>
        </div>
            <?php } ?>
    </div>
    <div class="space">
    </div>
    <div>
        <?php
        if($page == 1) {
            //echo'FIRST PREV';
        }
        else {
            $first_page = 1;
            echo"<a href=\"javascript:showMessagesOnPage('".$name_logon."','".$first_page."');\">FIRST</a>";
            $prevpage = $page - 1;
            echo' ';
            echo"<a href=\"javascript:showMessagesOnPage('".$name_logon."','".$prevpage."');\">PREV</a>";
        }
        echo" (Page $page of $last_page) ";
        if($page == $last_page) {
            //echo'NEXT LAST';
        }
        else {
            $nextpage = $page + 1;
            echo"<a href=\"javascript:showMessagesOnPage('".$name_logon."','".$nextpage."');\">NEXT</a>";
            echo' ';
            echo"<a href=\"javascript:showMessagesOnPage('".$name_logon."','".$last_page."');\">LAST</a>";
        }
        ?>
    </div>
</div>